home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / gethexc.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  1KB  |  61 lines

  1. /***********************************************************************
  2.  * $Id: gethexc.c,v 0.51 1993/07/23 22:00:37 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993 by T.C. Zhao                                    
  5.  *   All rights reserved.                                              
  6.  *.
  7.  *    Read a hex integer. Assuming a-f, A-F, 0-9 is continuous
  8.  ***********************************************************************/
  9.  
  10. #if !defined(lint) && defined(F_ID)
  11. char *id_ghex = "$Id: gethexc.c,v 0.51 1993/07/23 22:00:37 zhao Exp $";
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include "ulib.h"
  17.  
  18. #define IS_FS(c)      ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c)==',')
  19. #define IS_COMMENT(c) ((c) == '#')
  20.  
  21. int 
  22. gethexint(FILE * fp)
  23. {
  24.     static short hextab[256];
  25.     register int num = 0, i, c;
  26.  
  27. /* initialize the hex table */
  28.     if (!hextab['1']) 
  29.     {
  30.         for (i = '1'; i <= '9'; i++)
  31.             hextab[i] = i - '0';
  32.         for (i = 'A'; i <= 'F'; i++)
  33.             hextab[i] = 10 + i - 'A';
  34.         for (i = 'a'; i <= 'f'; i++)
  35.             hextab[i] = 10 + i - 'a';
  36.     }
  37.  
  38.     do {
  39.         c = getc(fp);
  40.         if (IS_COMMENT(c)) c = skip_comment(fp);
  41.     } while (IS_FS(c));
  42.  
  43. /*
  44.  * demand  0[xX]
  45.  */
  46.     if (c != '0' || ((c = getc(fp)) != 'x' && c != 'X')) 
  47.     {
  48.         bad_character(c);
  49.         return EOF;
  50.     }
  51. /*
  52.  * now do the coversion
  53.  */
  54.     do {
  55.         c = getc(fp);
  56.         num = (num << 4) + hextab[c];
  57.     } while (isxdigit(c));
  58.     return num;
  59. }
  60.  
  61.